#!/usr/local/bin/perl
#
# CommandLineCaller powered by CDYNE
# Written by apanagar and samirs
# This program makes use of CDYNE's phone services.
# Given the proper set of parameters, this program
# will deliver a message to another phone number
# from the commandline.
#
# This program can eaisly be worked into other scripts
# for notification purposes.

use LWP::UserAgent;
use HTTP::Request::Common;


if (@ARGV < 1){
    print "Error. No Command Line provided. Type -h or -help for help\n"
}


$licenseKey=0;
$xml_dump='false';

#do some commandline parsing
foreach my $command (@ARGV){
    if ($command =~ /^-h$/) { help_screen(); }
    elsif ($command =~ /^-help$/) { help_screen(); }
    elsif ($command =~ /^-x$/) { $xml_dump='true'; }
    elsif ($command =~ /^-lic\:(.*)$/) { $licenseKey = $1;}
    elsif ($command =~ /^-to\:([0-9]+)$/) { $phoneNumberToCall = $1; }
    elsif ($command =~ /^-from\:([0-9]+)$/) { $senderPhoneNumber = $1; }
    elsif ($command =~ /^-id\:(.*)$/) { $id = $1; }
    elsif ($command =~ /^-msg:(.*)$/) { $message = $1; }
}

#kill the program if the minimum req's are filled
if ($phoneNumberToCall eq ""){
  print "Please specify phone number to call with -to:<number>\n";
  exit;
}

if ($senderPhoneNumber eq ""){
  print "Please specify phone number to appear on the caller ID -from:<number>\n";
  exit;
}

if ($id eq ""){
  print "Please specify name to appear on the caller ID with -id:<text>\n";
  exit;
}

if ($message eq ""){
  print "Please specify the message with -msg:<text>\n";
  exit;
}

#if all the parameters are filled, then send the request
&send_request();



sub send_request(){

  $url='http://ws.cdyne.com/NotifyWS/phonenotify.asmx/NotifyPhoneBasic';

  #send the request
  my $browser = LWP::UserAgent->new;
  my $response = $browser->request(POST $url,
				   [
				    'LicenseKey'  => $licenseKey,
				    'phoneNumberToDial' => $phoneNumberToCall,
				    'CallerID' => $senderPhoneNumber,
				    'CallerIDname' => $id,
				    'VoiceID' => '0',
				    'TextToSay' => $message,
				    'cmdCall' => 'Make%20The%20Call'
				   ]
				  );
  die "Error: ", $response->status_line
    unless $response->is_success;

  #parse the response
  if ($response->content =~ /<ResponseCode>(.*)<\/ResponseCode>/){
    if ($1 eq "0"){
      print "Message sent\n";
    }
    elsif ($1 eq "10"){
      print "Demo license exceeded\n";}
    else{
      print "There was an error, turn on xml_dump for more information\n";
    }
  }

  #for the -x flag, if you want more info
  if ($xml_dump eq "true"){
    print $response->content;
  }

  print "\n\nUsing this software with inappropriate messages or harassment is in violation of FCC regulations. This software is used at your own risk and liability. IP addresses can be tracked, and CDYNE will cooperate with authorities if threatening messages are delivered through their systems. Please do not abuse the system.\n";
}


#print out the help screen
sub help_screen(){
  print "----\n";
  print "CommandLineCaller is powered by CDYNE\n";
  print "CommandLineCaller is in no way affiliated with CDYNE\n";
  print "\n";
  print "CommandLineCaller is provided with ABSOLUTELY NO WARRANTY.\n";
  print "Licensed under the GNU General Public License\n";
  print "This is free software, and you are allowed to\n";
  print "redistribute it under the conditions of the GNU GPL.\n";

  print "\nExample:  clc -to:4126485555 -from:4126484444 -id:\"Joe\" -msg:\"My name is Joe\"\n";

  print "\n+-----------------------------------+\n";
  print "-lic:<number>     Specify license number\n";
  print "                  (by default, set to 0 for demo)\n";
  print "-to:<number>      Specify number to which message should be delivered\n";
  print "-from:<number>    Specify number from which you are calling\n";
  print "-id:\"<text>\"      Specify caller id text\n";
  print "-msg:\"<text>\"     Specify the message\n";
  print "-x                XML dump for more info\n";
  print "+-----------------------------------+\n";
  print "\nFor more related information, visit:\n";
  print "http://www.cdyne.com\n";
  exit;
}
